package com.dupengcheng.subject.application.controller;


import com.alibaba.fastjson.JSONObject;
import com.dupengcheng.subject.application.convert.SubjectCategoryDTOConvert;
import com.dupengcheng.subject.application.convert.SubjectLabelDTOConvert;
import com.dupengcheng.subject.application.dto.SubjectCategoryDTO;
import com.dupengcheng.subject.application.dto.SubjectLabelDTO;
import com.dupengcheng.subject.common.entity.Result;
import com.dupengcheng.subject.common.enums.IsDeleteEnum;
import com.dupengcheng.subject.domain.convert.SubjectCategoryConvert;
import com.dupengcheng.subject.domain.entity.SubjectCategoryBO;
import com.dupengcheng.subject.domain.service.SubjectCategoryDomainService;
import com.dupengcheng.subject.infra.basic.entity.SubjectCategory;
import com.dupengcheng.subject.infra.basic.service.SubjectCategoryService;
import com.google.common.base.Preconditions;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;

@RequestMapping("/subject/category")
@RestController
@Slf4j
public class SubjectCategoryController {



    @Resource
    private SubjectCategoryDomainService subjectCategoryDomainService;

    @Resource
    private SubjectCategoryService categoryService;



    @PostMapping("/add")
    public Result<Boolean> add(@RequestBody SubjectCategoryDTO subjectCategoryDTO){
      try{
          if (log.isInfoEnabled()){
              log.info("SubjectCategoryController.add.dto:{}", JSONObject.toJSONString(subjectCategoryDTO));

          }
          Preconditions.checkNotNull(subjectCategoryDTO.getCategoryType(),"类型不能为空！");
          Preconditions.checkArgument(!StringUtils.isBlank(subjectCategoryDTO.getCategoryName()),"分类名称不能为空！");
          Preconditions.checkNotNull(subjectCategoryDTO.getParentId(),"分类父级不能为空！");
          SubjectCategoryBO subjectCategoryBO = SubjectCategoryDTOConvert.INSTANCE
                  .convertDTOtoBO(subjectCategoryDTO);
          subjectCategoryDomainService.add(subjectCategoryBO);
            return Result.ok();


        }catch (Exception e){
            log.error("SubjectCategoryController.add.error:{}",e.getMessage(),e);
            return Result.fail(e.getMessage());
        }
    }

    @PostMapping("/query")
    public Result<List<SubjectCategoryDTO>> query(@RequestBody SubjectCategoryDTO subjectCategoryDTO){
            try{
                if (log.isInfoEnabled()){
                    log.info("SubjectCategoryController.query.dto:{}", JSONObject.toJSONString(subjectCategoryDTO));

                }
                SubjectCategoryBO subjectCategoryBO = SubjectCategoryDTOConvert.INSTANCE.convertDTOtoBO(subjectCategoryDTO);
                List<SubjectCategoryBO> boList = subjectCategoryDomainService.query(subjectCategoryBO);
                List<SubjectCategoryDTO> resultList=SubjectCategoryDTOConvert.INSTANCE.convertBOtoDTOList(boList);

                return Result.ok(resultList);
            }catch (Exception e){
                log.error("SubjectCategoryController.query.error:{}",e.getMessage(),e);

                return Result.fail();
            }


    }

    @PostMapping("/update")
    public Result<Boolean> update(@RequestBody SubjectCategoryDTO subjectCategoryDTO){

        try{
            if (log.isInfoEnabled()){
                log.info("SubjectCategoryController.query.dto:{}", JSONObject.toJSONString(subjectCategoryDTO));

            }
            Preconditions.checkNotNull(subjectCategoryDTO.getId(),"分类ID不能为空！");
            SubjectCategoryBO subjectCategoryBO = SubjectCategoryDTOConvert.INSTANCE
                    .convertDTOtoBO(subjectCategoryDTO);
            Boolean flag=subjectCategoryDomainService.update(subjectCategoryBO);
            return Result.ok(flag);
        }catch (Exception e){
            log.error("SubjectCategoryController.update.error:{}",e.getMessage(),e);

            return Result.fail(e.getMessage());

        }



    }

    @PostMapping("/queryCategoryByPrimary")
    public Result<List<SubjectCategoryDTO>> queryCategoryByPrimary(@RequestBody SubjectCategoryDTO subjectCategoryDTO){

        try{
            if (log.isInfoEnabled()){
                log.info("SubjectCategoryController.queryCategoryByPrimary.dto:{}", JSONObject.toJSONString(subjectCategoryDTO));
            }
            SubjectCategoryBO subjectCategoryBO = SubjectCategoryDTOConvert.INSTANCE
                    .convertDTOtoBO(subjectCategoryDTO);
            List<SubjectCategoryBO> resultSubjectCategoryBO=subjectCategoryDomainService.queryCategoryByPrimary(subjectCategoryBO);
            List<SubjectCategoryDTO> subjectCategoryDTOS=new LinkedList<>();
            for (SubjectCategoryBO categoryBO : resultSubjectCategoryBO) {
                SubjectCategoryDTO categoryDTO=SubjectCategoryDTOConvert.INSTANCE
                        .convertBoToDTO(categoryBO);
                List<SubjectLabelDTO> subjectLabelDTOS = SubjectLabelDTOConvert.INSTANCE
                        .convertBOtoDTOList(categoryBO.getLabelList());
                categoryDTO.setLabelList(subjectLabelDTOS);
                subjectCategoryDTOS.add(categoryDTO);
            }
            return Result.ok(subjectCategoryDTOS);

        }catch (Exception e) {
            log.error("SubjectCategoryController.queryCategoryByPrimary.error:{}", e.getMessage(), e);
            return Result.fail(e.getMessage());
        }
    }

    @PostMapping("/delete")
    public Result<Boolean> delete(@RequestBody  SubjectCategoryDTO subjectCategoryDTO){
        try{
            if (log.isInfoEnabled()){
                log.info("SubjectCategoryController.delete.dto:{}",subjectCategoryDTO);
            }
            Preconditions.checkNotNull(subjectCategoryDTO.getId(),"ID不能为空");
           SubjectCategory subjectCategory= SubjectCategoryDTOConvert.INSTANCE
                    .convertDTOtoCategory(subjectCategoryDTO);
           subjectCategory.setIsDeleted(IsDeleteEnum.DELETE.getCode());
            boolean b= categoryService.update(subjectCategory)>0;
            return Result.ok(b);

        }catch (Exception e){
            log.error("SubjectCategoryController.delete.id:{}",e.getMessage(),e);
            return Result.fail("删除分类失败！");
        }

    }
}
